﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IRCPhase2Tester.Daemons;

namespace IRCPhase2Tester.Tests
{
    class FullNetworkNextHop : MultiClientsMultiDaemons
    {
        #region ITest Members

        public override string Title()
        {
            return "Full Network NextHop";
        }

        public override void BuildUp()
        {
            StartNewDaemon(1, Session.Instance.AllConfigurationFiles[1], typeof(ExternalRoutingDaemon));
            StartNewDaemon(2, Session.Instance.AllConfigurationFiles[2], typeof(ExternalRoutingDaemon));
            StartNewDaemon(3, Session.Instance.AllConfigurationFiles[3], typeof(ExternalRoutingDaemon));
            StartNewDaemon(4, Session.Instance.AllConfigurationFiles[4], typeof(ExternalRoutingDaemon));
            StartNewDaemon(5, Session.Instance.AllConfigurationFiles[5], typeof(ExternalRoutingDaemon));
            StartNewClient(this.daemons[0].IRCLocalPort);
            StartNewClient(this.daemons[1].IRCLocalPort);
            StartNewClient(this.daemons[2].IRCLocalPort);
            StartNewClient(this.daemons[3].IRCLocalPort);
            StartNewClient(this.daemons[4].IRCLocalPort);

            base.BuildUp();
        }

        public override bool RunTest()
        {
            clients[0].Send("adduser test1");
            if (clients[0].Receive().ToLower().Trim() != "ok")
            {
                return false;
            }

            clients[4].Send("adduser test2");
            if (clients[4].Receive().ToLower().Trim() != "ok")
            {
                return false;
            }

            System.Threading.Thread.Sleep(5000);

            int attempts = 10;
            while (attempts-- > 0)
            {
                clients[0].Send("NextHop test2");
                if (clients[0].Receive().ToLower().Trim() == "ok 3 2")
                {
                    return true;
                }
                System.Threading.Thread.Sleep(10000);
            }

            return false;
        }

        #endregion
    }
}
